-
Notifications
You must be signed in to change notification settings - Fork 13
Ignore AttributeError when shutting down server #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you @tjni! It seems strange that |
Thank you for the fast review. Would it be all right to add a test that mocks Indeed the construction of the object fails when |
Ah, I see, thanks. I had forgotten about I think mocking |
83e9795
to
4f01874
Compare
I tried to write a test and realized why it's hard. Any suggestions welcome!
This unfortunately doesn't work because we cannot guarantee that the finalizer runs by the time we assert. When I test locally, the finalizer only runs after all the tests finish. |
Hmm yeah I see what you mean. Off the top of my head, what about putting the server-manipulation code in a helper function? Maybe stack unwinding will trigger the finalizer to run. def fail_to_create_server():
with patch("pytest_localserver.http.make_server", side_effect=Exception("init failure")):
# Call this just to trigger an exception during construction of the ContentServer
server = http.ContentServer()
def test_httpserver_init_failure_no_warning_during_cleanup(capfd):
try:
fail_to_create_server()
except Exception:
pass
assert capfd.readouterr().err == "" If that doesn't work, you could try running I'll return to this issue tomorrow or later this week and let you know if I think of anything else. |
Oh BTW I'd probably suggest using a more specific error type when mocking - maybe And that |
Thank you! I will also take a closer look tomorrow morning and try out your suggestions.
|
Ahh, gotcha. In that case it should be fine. Personally I've tried to avoid mixing different test frameworks (i.e. pytest and unittest) just to keep the code easier to understand, but it's not really hurting much to do so, and if it results in cleaner code to use something from unittest then that's a win. |
4f01874
to
288a490
Compare
I tried several ways to force
My current approach is to try to do the next best thing, which is to simulate the scenario by deleting the |
Hmm, okay, well if necessary we can work with that. Is the code where you originally encountered this error something that you can share? Or, could you make an equivalent example that you'd be able to share? Even if it's something complex, I think we might be able to distill it down into something that will reproduce the error - I'd at least like to try, if possible. |
Great idea! If you have a macOS system (or runner), here's how you can reproduce it. Start in an environment with
You can verify that running:
works just fine and exits with status code 0. However, running the following does not:
|
Fantastic, thanks! I actually don't have a Mac, but I managed to reproduce the failure by just wrapping the whole body of the script in from pytest_localserver import http
from unittest.mock import patch
with patch("pytest_localserver.http.make_server", side_effect=Exception("init failure")):
server = http.ContentServer()
server.start()
server.stop() I think it would be totally fine to just run that in a test as a subprocess. I mean, I'll keep looking for a way to do it as an in-process test with pytest, and feel free to do the same, but a test with a separate script run as a subprocess is a good "checkpoint" and enough to proceed with the PR as far as I'm concerned. The hack of deleting the |
288a490
to
b4b36ef
Compare
I rewrote the test based on your suggestion, using a subprocess. Please take a look :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Just a couple changes I'd suggest, and then we should be able to get this wrapped up.
d98576e
to
93a5e75
Compare
It's possible for the _server attribute to not exist if make_server raises an error in the constructor.
93a5e75
to
fb43a54
Compare
Looks good so far 😄 Just double-checking, should I go ahead and merge this once the workflow runs complete, assuming they all pass? Or did you want to do anything else with it first? (I don't know if Github will let you merge it yourself since I've approved the changes, but if it will, feel free to do so if you want) |
Feel free to merge it, thank you for all of your help. |
And thank you for the fix! |
It's possible for the _server attribute to not exist if make_server raises an error in the constructor. This usually emits some sort of additional error onto the console that can distract from identifying the real issue.